package com.stockz.equity.charting.util;

import java.awt.Component;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;

import org.metawidget.swing.SwingMetawidget;

import com.stockz.equity.charting.display.Displayable;
import com.stockz.equity.charting.model.Point;
import com.stockz.equity.charting.view.StockChart;
import com.stockz.equity.charting.view.StockChartPanelState;
import com.stockz.equity.charting.view.StockChartView;
import com.stockz.equity.charting.view.widget.EditIndicatorPopupMenu;
import com.stockz.equity.charting.view.widget.custom.MultiSplitPane;
import com.stockz.equity.charting.view.widget.custom.MultiSplitLayout.Divider;
import com.stockz.equity.charting.view.widget.custom.MultiSplitLayout.Leaf;
import com.stockz.equity.charting.view.widget.custom.MultiSplitLayout.Node;
import com.stockz.equity.charting.view.widget.custom.MultiSplitLayout.Split;

public class SwingUtil {

	public static Rectangle getMaxAreaRectangle(Rectangle[] rectangles){
		Rectangle result = new Rectangle();
		if(rectangles != null && rectangles.length != 0){
			for(Rectangle rectangle : rectangles){
				if(result.width*result.height < rectangle.width*rectangle.height){
					result = rectangle;
				}
			}
		}
		return result;
	}
	
	public static Point unfitPoint(StockChartPanelState state, java.awt.Point point){
		return new Point(
		MathUtil.unfitY(state.getPriceRectangle().y, 
				state.getPriceRectangle().y + state.getPriceRectangle().height, 
				state.getMinimumPriceVisible(), 
				state.getMaximumPriceVisible(), 
				point.y,
				state.isSemiLog()),
				
		MathUtil.unfitX(state.getPriceRectangle().x, 
				state.getTimeUnitSpace(), 
				state.getEndIndex(), 
				point.x));
	}
	
	public static java.awt.Point fitPoint(StockChartPanelState state, Point point){
		return new java.awt.Point(
		MathUtil.fitX(state.getPriceRectangle().x, 
				state.getTimeUnitSpace(), 
				state.getEndIndex(), 
				point.getIndex()),
				
		MathUtil.fitY(state.getPriceRectangle().y, 
				state.getPriceRectangle().y + state.getPriceRectangle().height, 
				state.getMinimumPriceVisible(), 
				state.getMaximumPriceVisible(), 
				point.getPrice(),state.isSemiLog()));
		
	}
	
	public static void restrictPoint(java.awt.Point point,Rectangle rectangle){
		if(!rectangle.contains(point)){
			if(point.x < rectangle.x)
				point.x = rectangle.x;
			if(point.y < rectangle.y)
				point.y = rectangle.y;
			if(point.x > rectangle.x+rectangle.width)
				point.x = rectangle.x+rectangle.width;
			if(point.y > rectangle.y+rectangle.height)
				point.y = rectangle.y+rectangle.height;
		}
	}
	
	
	public static void restrictPointOutside(java.awt.Point point,Rectangle rectangle){
		if(!rectangle.contains(point)){
			if(point.x < rectangle.x)
				point.x = rectangle.x - 5;
			if(point.y < rectangle.y)
				point.y = rectangle.y - 5;
			if(point.x > rectangle.x+rectangle.width)
				point.x = rectangle.x+rectangle.width + 5;
			if(point.y > rectangle.y+rectangle.height)
				point.y = rectangle.y+rectangle.height + 5;
		}
	}
	
public static Component split(List<? extends Component> componentList){
	if(componentList == null || componentList.isEmpty()){
		return null;
	}else if(componentList.size() == 1){
		return componentList.get(0);
	}else if(componentList.size() == 2){
		return new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
				componentList.get(0),componentList.get(1));
	}
	
		int x = 1;
		int y = 1;
		int count = componentList.size();
		
		while(x*y < count){
			y++;
			if(x*y <count){
				x++;
			}else{
				break;
			}
		}
		
		Split mainSplit = new Split();
		mainSplit.setRowLayout(true);
		List<Node> splitList = new ArrayList<Node>();
		
		Divider extraSplitDivider = null;
		for(int i = 0; i < y ; i++){
			Split split = new Split();
			split.setRowLayout(false);
			List<Node> nodeList = new ArrayList<Node>();
			Divider extraLeafDivider = null;
			for(int j = 0; j < x; j++){
				Leaf leaf = new Leaf(String.valueOf(i)+j);
				nodeList.add(leaf);
				extraLeafDivider = new Divider();
				nodeList.add(extraLeafDivider);
			}
			nodeList.remove(extraLeafDivider);
			split.setChildren(nodeList);
			splitList.add(split);
			extraSplitDivider = new Divider();
			splitList.add(extraSplitDivider);
		}
		splitList.remove(extraSplitDivider);
		
		mainSplit.setChildren(splitList);
		MultiSplitPane multiSplitPane = new MultiSplitPane();
		multiSplitPane.getMultiSplitLayout().setModel(mainSplit);
		
		count = 0;
		for(int i = 0; i < y ; i++){
			for(int j = 0; j < x; j++){
				if(count >= componentList.size()){
					break;
				}
				Component component = componentList.get(count);
				multiSplitPane.add(component,String.valueOf(i)+j);
				count++;
			}
		}
		return multiSplitPane;
	}

	
}
